package cn.tianya.irock.view;

import java.text.MessageFormat;

import javax.servlet.http.Cookie;

import cn.tianya.irock.bll.Roles;
import cn.tianya.irock.bll.Users;
import cn.tianya.irock.core.Page;
import cn.tianya.irock.core.SiteUrlsBase;
import cn.tianya.irock.data.model.User;
import cn.tianya.irock.enums.ApplicationExceptionType;
import cn.tianya.irock.enums.CreateUserStatus;
import cn.tianya.irock.enums.LoginUserStatus;
import cn.tianya.irock.exceptions.ApplicationException;
import cn.tianya.irock.security.FormsAuthentication;


public class LoginPage extends Page {
	
	protected void validaAccess() {}
	
	public void show() {
		String target = request.getParameter("t");
		String login_url = null;
		String sys_login_url = null;
		
		login_url = MessageFormat.format(config.getAppLoginUrl(), 
											config.getDomain() + 
											config.getAppRoot() + 
											"/" +
											config.getDefaultApp() + 
											"/" +
											config.getSiteUrls().getRawUrl(
													config.getDefaultApp(), 
													"login.show").rawPath() +
											"?t=login");
		sys_login_url = config.getAppRoot() + 
						"/" +
						config.getDefaultApp() + 
						"/" +
						config.getSiteUrls().getRawUrl(config.getDefaultApp(), "login.show").rawPath();
		
		if (target == null) {
			response.setRedirect(login_url);
		} else if (target.equals("login")) {
			try {
				login();
			} catch(ApplicationException ex) {
				response.setRedirect(sys_login_url + "?t=create");
			}
		} else if (target.equals("logout")) {
			logout();
		} else if (target.equals("create")) {
			createUser();
			response.setRedirect(sys_login_url + "?t=login");
		} else {
			response.setRedirect(login_url);
		}
	}
	
	protected void createUser() {
		CreateUserStatus status = null;
		
		status = Users.create(Users.getTianyaUsername());
		if (status != CreateUserStatus.Created && status != CreateUserStatus.DuplicateUsername) {
			throw new ApplicationException(ApplicationExceptionType.UnknowError, "on AuthPage::initial CreateUser Error!");
		}
	}

	protected void login() {
		Cookie[] cookies = null;
		LoginUserStatus loginStatus = null;
		
		cookies = request.getCookies();
		loginStatus = Users.validUser(cookies);
		
		if (loginStatus == LoginUserStatus.InvalidCredentials) { 
			throw new ApplicationException(ApplicationExceptionType.UserInvalidCredentials);
		} else if (loginStatus == LoginUserStatus.AccountPending) { 
			throw new ApplicationException(ApplicationExceptionType.UserAccountPending);
		} else if (loginStatus == LoginUserStatus.AccountBanned) { 
			throw new ApplicationException(ApplicationExceptionType.UserAccountBanned);
		} else if (loginStatus == LoginUserStatus.AccountDisapproved) { 
			throw new ApplicationException(ApplicationExceptionType.UserAccountDisapproved);
		} else if (loginStatus == LoginUserStatus.UnknownError) { 
			throw new ApplicationException(ApplicationExceptionType.UserUnknownLoginError);
		}
		SiteUrlsBase urls = (SiteUrlsBase)config.getSiteUrls();
		response.setRedirect(urls.message(ApplicationExceptionType.LoginSuccess));
	}
	
	protected void logout() {
		setTemplateName("login.loginout");
		FormsAuthentication.signOut();
		Roles.signOut();
		SiteUrlsBase urls = (SiteUrlsBase)config.getSiteUrls();
		response.setRedirect(urls.message(ApplicationExceptionType.LogoutSuccess));
	}

}
